<%@ page language="java" pageEncoding="UTF-8"%>
<%@ page import= "com.roadmap.common.BaseConst"%>
<%@ page import="com.roadmap.struts.action.ActionConst"%>
<%@ page import="com.roadmap.common.RdcResourceBundle" %>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html"%>
<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean"%>
<%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
String user_ssn_id = (String) request.getSession().getAttribute(ActionConst.Aspirant_Id_In_Session);
String user_ssn_name = (String) request.getSession().getAttribute(ActionConst.Aspirant_Name_In_Session);
String action = request.getParameter(ActionConst.Method_Symbol);
if(action == null || (!action.equals("create") && !action.equals("view"))) {
	action = "create";
}
String dialogId = request.getParameter(ActionConst.Item_Id_In_Request);
%>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<base href="<%=basePath%>" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta http-equiv="pragma" content="no-cache"/>
<meta http-equiv="cache-control" content="no-cache"/>
<meta http-equiv="expires" content="0"/> 
<link href="res/css/layout.css" rel="stylesheet" type="text/css" media="all" />
<link href="res/css/sns.css" rel="stylesheet" type="text/css" media="all" />
<link href="res/css/widget.css" rel="stylesheet" type="text/css" media="all" />
<script type="text/javascript" src="dwr/util.js"></script>
<script type="text/javascript" src="dwr/engine.js"></script>
<script type="text/javascript" src="dwr/interface/RdcAjax.js"></script>
<script type="text/javascript" src="res/js/func.js"></script>
<script type="text/javascript" src="res/js/widget.js"></script>
<title>短消息 | <%=RdcResourceBundle.COMPANY_NAME %></title>

</head>

<body>
	<jsp:include page="/admin/myheader.jsp"></jsp:include>
	
	<!-- define the frame for logging on-->
	<div class="mainbody"><div class="manager">
	<jsp:include page="/admin/msg/nav_btns.jsp"/>
	
	<!-- middle part: content -->
	<div class="middle-part">
		<div class="section">
			<div class="title-sp" style="border:none;">短消息</div>
			<div class="navg">
				<ul id="msgBox">
					<li><a href="message.do?act=all&msg=inbox" onfocus="this.blur();">收件箱</a></li>
					<li><a href="message.do?act=all&msg=sent" onfocus="this.blur();">发件箱</a></li>
					<% if(action.equalsIgnoreCase("view")) {%>
					<li><a href="message.do?act=create" onfocus="this.blur();" onclick="">发新短消息</a></li>
					<%} %>
				</ul>
			</div>
		</div>
		<% if(action.equals("create")) {%>
		<div id="createMsg" class="section">
			<h4>创建新短消息</h4>
			<table class="msgtable">
				<tr><td></td><td>收件人列表</td><td></td><td>好友列表</td></tr>
				<tr><td>发送给</td>
					<td><select id="receiverList" size="5" multiple title="按住Ctrl或Shift选择多个"></select></td>
					<td>
						<input type="image" src="res/images/logo/leftarrow.png" onclick="addReceiver();"/><br/><br/>
						<input type="image" src="res/images/logo/rightarrow.png" onclick="removeReceiver();"/>
					</td>
					<td>
						<select id="friendList" size="5" multiple title="按住Ctrl或Shift选择多个">
							<logic:present name="friends">
							<logic:iterate id="friend" name="friends">
							<option value="<bean:write name='friend' property='friendId'/>"><bean:write name='friend' property='name'/></option>
							</logic:iterate>
							</logic:present>
						</select>
					</td>
				</tr>
				<tr><td>内容</td><td colspan="3">
					<html:form action="/message" onsubmit="return sendMsg();">
					<div class="inputtd">
						<textarea name="content" class="input-text" style="width:340px;height:120px;" title="消息内容(1~210字)"></textarea>
						<input type="hidden" name="act" value="edit"/>
						<input name="recipientIDs" type="hidden" value=""/>
					</div>
					<div class="submittd"><input type="submit" class="confirmBtn" value="确定"/></div>
					</html:form>
				</td></tr>
			</table>
		</div>
		<%} else if (action.equals("view")) {%>
		<div id="viewMsg" class="section">
			<logic:notPresent name="dialog"><span class="prompt-message">没有该对话，可能已被删除</span></logic:notPresent>
			<logic:present name="dialog">
			<h4><a href="aspirant.do?uid=<bean:write name='dialog' property='sponsorId'/>"><bean:write name='dialog' property='sponsorName'/></a>和<a href="aspirant.do?uid=<bean:write name='dialog' property='participatorId'/>"><bean:write name='dialog' property='participatorName'/></a>之间的对话</h4>
			<logic:present name="messages">
			<div class="message">
				<ul id="messageList">
					<li id="tempNode" style="display:none;">
						<div class="smallpicture"><a href="aspirant.do?uid=<%=user_ssn_id %>"><img id="img" src="<%=BaseConst.PURIKURA_DIR_SMALL%><%=user_ssn_id %>.gif" alt="<%=RdcResourceBundle.COMPANY_NAME %>"/></a></div>
						<div class="author">
							<a href="aspirant.do?uid=<%=user_ssn_id %>"><%=user_ssn_name%></a><br/><em id="msgDate"></em>
						</div>
						<div id="msgContent" class="content"></div>
					</li>
					
					<logic:iterate id="msg" name="messages">
					<bean:define id="disid" name="msg" property="dispatcherId"/>
					<li>
						<div class="smallpicture"><a href="aspirant.do?uid=<%=disid%>"><img src="<%=BaseConst.PURIKURA_DIR_SMALL%><%=disid %>.gif" alt="<%=RdcResourceBundle.COMPANY_NAME %>"/></a></div>
						<div class="author">
							<a href="aspirant.do?uid=<%=disid %>"><bean:write name='msg' property='dispatcherName'/></a><br/>
							<em><bean:write format='yyyy-MM-dd HH:mm' name='msg' property='createdTime'/></em>
						</div>
						<div class="content"><bean:write name='msg' property='content'/></div>
					</li>
					</logic:iterate>
				</ul>
			</div>
			</logic:present>
			<table class="msgtable" style="float:right;">
				<tr><td><strong>回复</strong></td></tr>
				<tr><td>
					<div class="inputtd"><textarea id="replyContent" class="input-text" style="width:410px;height:120px;" title="消息内容(1~210字)"></textarea></div>
					<div class="submittd"><input id="sendMsgBtn" type="button" class="confirmBtn" value="确定" onclick="reply('<%=dialogId %>');"/></div>
				</td></tr>
			</table>
			</logic:present>
		</div>
		<%} %>
	</div>
	
	<!-- right part: -->
	<div class="right-part">
	</div>
	
	</div>
	
	<div class="clear-div"></div>
	</div>
	
	<jsp:include page="/res/jsp/footer.jsp"></jsp:include>

<% if(action.equals("create")) {%>
<script type="text/javascript">
	window.onload = function(){
		list = $("friendList");
		if(list !== null && list.options.length > 0) {
			list.focus();
			list.selectedIndex = 0;
		}
	}
	
	function addReceiver() {
		moveObjects("friendList", "receiverList" , "请从好友列表选择一个好友");
	}
	
	function removeReceiver() {
		moveObjects("receiverList", "friendList" , "请从收件人列表选择一个要移除的对象");
	}
	
	function moveObjects(from, to, msg) {
		var list = $(from);
		var selected = false;
		for(i = 0; i < list.options.length; i += 1) {
			op = list.options[i];
			if(op.selected) {
				selected = true;
				$(to).appendChild(op);
				op.selected = false;
				i -= 1;
			}
		}
		if(!selected) {
			PromptDialog.show(msg);
		}
		if(list.options.length > 0) {
			list.focus();
			list.selectedIndex = 0;
		}
	}
	
	function sendMsg() {
		var list = $("receiverList");
		var receivers = "";
		for(i = 0; i < list.length; i += 1) {
			receivers += list[i].value + ",";
		}
		if(receivers === "") {
			PromptDialog.show("请从好友列表中选择好友添加到收件人列表中");
			return false;
		}
		$("recipientIDs").value = receivers;
		if(!RDC.util.verifyText($("content").value, 1, 210)) {
			PromptDialog.show("消息内容的字数为1~210");
			return false;
		}
		return true;
	}
</script>
<%} else { %>
<script type="text/javascript">
	window.onload = function() {
		$("replyContent").focus();
	}
	
	function reply(digid) {
		if(digid === null || typeof digid === "undefined") {
			return;
		}
		content = $("replyContent").value;
		if(!RDC.util.verifyText(content, 1, 210)) {
			PromptDialog.show("消息内容的字数为1~210");
			return;
		}
		
		RDC.gui.disable("sendMsgBtn");
		RdcAjax.createMessage(digid, content, function(result) {
			if(typeof result.id !== "undefined") {
				node = RDC.gui.cloneNode($("tempNode"), {idSuffix:result.id});
				$("messageList").appendChild(node);
				$("msgContent" + result.id).innerHTML = content;
				$("msgDate" + result.id).innerHTML = RDC.util.dateToString(result.createdTime, "yyyy-MM-dd HH:mm");
				// clean
				$("replyContent").value = "";
				
				PromptDialog.show("成功发送消息");
			}
			else {
				PromptDialog.show("发送消息失败，由于" + result);
			}
			RDC.gui.enable("sendMsgBtn");
		});
	}
</script>
<%} %>
	
</body>

</html>
